package com.lejin.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lejin.JsonUtils;
import com.lejin.JwtUtils;
import com.lejin.RsaUtils;

import com.lejin.user.mapper.SysUserMapper;
import com.lejin.user.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lejin.usercenter.entity.SysUser;
import com.lejin.vo.ResultVO;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.security.PrivateKey;


@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public ResultVO login(SysUser user) {
        if (user == null) {
            return new ResultVO(false, "用户名或密码不能为空!");
        }

        //获取用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", user.getAccount());
        queryWrapper.eq("user_type", "6");
        SysUser sysUserFromDB = this.baseMapper.selectOne(queryWrapper);
        if (sysUserFromDB == null) {
            return new ResultVO(false, "用户名或密码错误!1");
        }

        //核对密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(user.getPassword(), sysUserFromDB.getPassword())) {
            return new ResultVO(false, "用户名或密码错误!2");
        }

        //jwt令牌
        //私钥加密
        try {
            String path = ResourceUtils.getFile("classpath:rsa_pri").getPath();
            PrivateKey privateKey = RsaUtils.getPrivateKey(path);
            sysUserFromDB.setPassword("");
            //颁发token
            String token = JwtUtils.generateTokenExpireInMinutes(sysUserFromDB, privateKey, 40);
            return new ResultVO(true, "success", token);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "用户名或密码错误!3");

        }
    }


    @Override
    public ResultVO login2(String phone, String code) {
        return null;
    }

    @Override
    public ResultVO register(SysUser user) {

        if (user.getAccount()==null||user.getPassword()==null) {
            return new ResultVO(false, "用户名或密码不能为空!");
        }

        //获取用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", user.getAccount());
        SysUser sysUserFromDB = this.baseMapper.selectOne(queryWrapper);
        if (StringUtils.isEmpty(JSONUtil.toJsonStr(sysUserFromDB))) {
            //todo注册
            //核对密码
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encode = encoder.encode(user.getPassword());
            user.setPassword(encode);


            int insert = this.baseMapper.insert(user);

            if (insert>0){
              return ResultVO.SUCCESS(true,"success","注册成功");
            }


            return new ResultVO(false, "注册失败");
        }




        return ResultVO.FAIL(false,"fail","已有用户名");
    }
}
